#include <algorithm>
#include <cstdio>
#include <cstring>

typedef long long ll;
const int N = 100005, mod = 20130427;
int n, m, B;
int L[N], R[N];
ll S[N];
ll a[N][2], ans[N][2], ss[N][2], sl[N][2];

int solve(int *p, int l) {
  memset(a, 0, sizeof(a));
  memset(ans, 0, sizeof(ans));
  memset(ss, 0, sizeof(ss));
  memset(sl, 0, sizeof(sl));
  a[l][0] = 1;
  for (int i = l - 1; ~i; --i) {
    int c = (i == l - 1 ? 0 : B);
    a[i][0] = a[i + 1][0];
    a[i][1] = (c - 1 + a[i + 1][1] * B + a[i + 1][0] * p[i]) % mod;
    sl[i][0] = sl[i + 1][0] + a[i + 1][0];
    sl[i][1] = (c - 1 + sl[i][0] * p[i] + (sl[i + 1][1] + a[i + 1][1]) * B) % mod;
    ss[i][0] = (ss[i + 1][0] * B + p[i] * sl[i][0]) % mod;
    ss[i][1] = (S[c] + ss[i + 1][0] * B * p[i] + S[p[i]] * sl[i][0] +
                ss[i + 1][1] * B % mod * B + S[B] * (sl[i + 1][1] + a[i + 1][1])) %
               mod;
    ans[i][0] = (ans[i + 1][0] + ss[i][0]) % mod;
    ans[i][1] = (ans[i + 1][0] * p[i] + ans[i + 1][1] * B + ss[i][1]) % mod;
  }
  return (ans[0][0] + ans[0][1]) % mod;
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("P3281 [SCOI2013]数数.in", "r", stdin);
  freopen("P3281 [SCOI2013]数数.out", "w", stdout);
#endif
#endif

  scanf("%d", &B);
  S[0] = 0;
  for (int i = 0; i < B; ++i) S[i + 1] = (S[i] + i) % mod;
  scanf("%d", &n);
  for (int i = n - 1; ~i; --i) scanf("%d", &L[i]);
  
  for (int i = 0; i < n; ++i) {  // process L - 1
    if (L[i] > 0) {
      --L[i];
      break;
    }
    L[i] = B - 1;
  }
  if (!L[n - 1]) --n;

  scanf("%d", &m);
  for (int i = m - 1; ~i; --i) scanf("%d", &R[i]);
  printf("%d\n", (solve(R, m) - solve(L, n) + mod) % mod);
  return 0;
}